package com.example.admin.controller.sys;

import com.example.admin.entity.SysDictType;
import com.example.shrio.CurrentUser;
import com.example.util.page.SeachParamsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysDictInfo;
import com.example.admin.service.ISysDictInfoService;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.page.Pager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典信息表 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@RestController
@Api(value = "字典参数Controller", tags = {"字典参数操作接口" })
@RequestMapping("/sys/sysDictInfo")
public class SysDictInfoController {

    @Autowired
    private ISysDictInfoService service;

    /**
     * 获取参数
     *
     * @param pager
     * @return
     */
    @ApiOperation(value = "字典参数列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public AjaxResult page(@RequestBody Pager pager) {
        Page<SysDictInfo> page = new Page<>();
        page.setCurrent(pager.getPageNum());
        page.setSize(pager.getPageSize());
        QueryWrapper<SysDictInfo> queryWrapper = new QueryWrapper<>();
        SeachParamsUtil.loadSearchParam(pager, queryWrapper);
        pager.convertPage(service.page(page, queryWrapper));
        return new AjaxResult(APIState.S00, pager);
    }

    /**
     * 保存
     *
     * @param dictInfo
     * @return
     */
    @ApiOperation(value = "字典参数新增", httpMethod = "POST")
    @PostMapping
    public AjaxResult save(@RequestBody SysDictInfo dictInfo) {
        if (dictInfo != null) {
            dictInfo.setStatus(0);
            dictInfo.setCreateTime(new Date());
            dictInfo.setCreateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
            if (dictInfo.insert()) {
                return new AjaxResult(APIState.S00, "保存成功");
            }
        }
        return new AjaxResult(APIState.S01, "保存失败");
    }

    /**
     * 修改
     *
     * @param dictInfo
     * @return
     */
    @ApiOperation(value = "字典参数编辑", httpMethod = "PUT")
    @PutMapping
    public AjaxResult update(@RequestBody SysDictInfo dictInfo) {
        if (dictInfo != null && dictInfo.getId() != null) {
            dictInfo.setUpdateTime(new Date());
            dictInfo.setUpdateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
            if (dictInfo.insertOrUpdate()) {
                return new AjaxResult(APIState.S00, "保存成功");
            }
        }
        return new AjaxResult(APIState.S01, "保存失败");
    }

    /**
     * 刪除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "字典参数删除", httpMethod = "DELETE")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        if (service.removeByIds(Arrays.asList(ids))) {
            return new AjaxResult(APIState.S00, "刪除成功");
        }
        return new AjaxResult(APIState.S01, "删除失败");
    }
    //获取类型数据值
    @ApiOperation(value = "字典管理对应参数", httpMethod = "GET")
    @GetMapping("/{dictType}")
    public AjaxResult list(@PathVariable String dictType) {
        SysDictInfo dictInfo = new SysDictInfo();
        dictInfo.setDictType(dictType);
        QueryWrapper<SysDictInfo> queryWrapper = new QueryWrapper<SysDictInfo>(dictInfo);
        List<SysDictInfo> list = service.list(queryWrapper);
        if (list != null && list.size() > 0) {
            return new AjaxResult(APIState.S00, list.stream().map(info -> {
                JSONObject json = new JSONObject();
                json.put("dictLabel", info.getFieldLabel());
                json.put("dictValue", info.getFieldVal());
                return json;
            }).collect(Collectors.toList()));
        }
        return new AjaxResult(APIState.S00, new ArrayList<>());
    }

    @ApiOperation(value = "字典参数启用/停用", httpMethod = "PUT")
    @PutMapping("start")
    public AjaxResult start(Integer id) {
        SysDictInfo dictInfo = service.getById(id);
        if (dictInfo != null) {
            if (dictInfo.getStatus() == 1) {
                dictInfo.setStatus(0);
            } else {
                dictInfo.setStatus(1);
            }
            dictInfo.setUpdateTime(new Date());
            dictInfo.setUpdateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
            if (service.updateById(dictInfo)) {
                return new AjaxResult(APIState.S00, "更改成功");
            }
        }
        return new AjaxResult(APIState.S01, "更改失败");
    }

}
